import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _forEach from "lodash/forEach";
import { createPagination, getResponse } from 'utils/utils';
import { queryIdpValue } from 'cpaas-front/lib/services/api';
import { uiPageSiteCreate, uiPageSiteQueryPaging, uiPageSiteUpdate, // detail
uiPageSiteQueryDetail, uiPageSiteDetailUpdate } from "../services/uiPageService";
export default {
  namespace: 'uiPage',
  state: {
    // 列表
    list: {},
    pagination: {},
    // 详情
    config: {},
    // 值集
    detailIdp: {}
  },
  effects: {
    fetchList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchList(_ref, _ref2) {
      var payload, put, call, res, list;
      return _regeneratorRuntime.wrap(function fetchList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              put = _ref2.put, call = _ref2.call;
              _context.next = 4;
              return call(uiPageSiteQueryPaging, payload);

            case 4:
              res = _context.sent;
              list = getResponse(res);

              if (!list) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  list: list,
                  pagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchList);
    }),
    listCreateOne: /*#__PURE__*/_regeneratorRuntime.mark(function listCreateOne(_ref3, _ref4) {
      var payload, call;
      return _regeneratorRuntime.wrap(function listCreateOne$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.t0 = getResponse;
              _context2.next = 5;
              return call(uiPageSiteCreate, payload);

            case 5:
              _context2.t1 = _context2.sent;
              return _context2.abrupt("return", (0, _context2.t0)(_context2.t1));

            case 7:
            case "end":
              return _context2.stop();
          }
        }
      }, listCreateOne);
    }),
    listUpdateOne: /*#__PURE__*/_regeneratorRuntime.mark(function listUpdateOne(_ref5, _ref6) {
      var payload, call;
      return _regeneratorRuntime.wrap(function listUpdateOne$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.t0 = getResponse;
              _context3.next = 5;
              return call(uiPageSiteUpdate, payload);

            case 5:
              _context3.t1 = _context3.sent;
              return _context3.abrupt("return", (0, _context3.t0)(_context3.t1));

            case 7:
            case "end":
              return _context3.stop();
          }
        }
      }, listUpdateOne);
    }),
    fetchDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDetail(_ref7, _ref8) {
      var payload, put, call, config, componentType, componentTypeMeaning;
      return _regeneratorRuntime.wrap(function fetchDetail$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              put = _ref8.put, call = _ref8.call;
              _context4.t0 = getResponse;
              _context4.next = 5;
              return call(uiPageSiteQueryDetail, payload);

            case 5:
              _context4.t1 = _context4.sent;
              config = (0, _context4.t0)(_context4.t1);
              _context4.t2 = getResponse;
              _context4.next = 10;
              return call(queryIdpValue, 'CPFM.UI.COMPONENT');

            case 10:
              _context4.t3 = _context4.sent;
              componentType = (0, _context4.t2)(_context4.t3);

              if (!config) {
                _context4.next = 15;
                break;
              }

              _context4.next = 15;
              return put({
                type: 'updateState',
                payload: {
                  config: config
                }
              });

            case 15:
              if (!componentType) {
                _context4.next = 20;
                break;
              }

              componentTypeMeaning = {};

              _forEach(componentType, function (idp) {
                componentTypeMeaning[idp.value] = idp.meaning;
              });

              _context4.next = 20;
              return put({
                type: 'updateDetailIdp',
                payload: {
                  componentType: componentType,
                  componentTypeMeaning: componentTypeMeaning
                }
              });

            case 20:
            case "end":
              return _context4.stop();
          }
        }
      }, fetchDetail);
    }),
    detailUpdate: /*#__PURE__*/_regeneratorRuntime.mark(function detailUpdate(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function detailUpdate$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.t0 = getResponse;
              _context5.next = 5;
              return call(uiPageSiteDetailUpdate, payload);

            case 5:
              _context5.t1 = _context5.sent;
              res = (0, _context5.t0)(_context5.t1);
              return _context5.abrupt("return", res);

            case 8:
            case "end":
              return _context5.stop();
          }
        }
      }, detailUpdate);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref11) {
      var payload = _ref11.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    updateDetailIdp: function updateDetailIdp(state, _ref12) {
      var payload = _ref12.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        detailIdp: _objectSpread(_objectSpread({}, state.detailIdp), payload)
      });
    }
  }
};